package zirao.base.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;

/**
 * Created by ${lixiaonan} on 2018/7/31.
 */
public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter {
    LinkedList<Object> linkedList = new LinkedList<Object>();
    OnItemClickListener onItemClickListener;
    OnItemLongClickListener onItemLongClickListener;

    public BaseRecyclerAdapter() {
    }

    public BaseRecyclerAdapter(LinkedList<Object> linkedList) {
        setList(linkedList);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return getViewHolder(parent, viewType);
    }

    protected abstract RecyclerView.ViewHolder getViewHolder(ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder != null && holder.itemView != null) {
            final BaseRecyclerVH baseRecyclerVH = (BaseRecyclerVH) holder;
            setVHData(holder, linkedList.get(position), position);
            baseRecyclerVH.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onItemClickListener) {
                        onItemClickListener.onItemClick(baseRecyclerVH.itemView, linkedList.get(position),
                                position, baseRecyclerVH.getItemId());
                    }
                }
            });
            baseRecyclerVH.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (null != onItemLongClickListener) {
                        onItemLongClickListener.onItemLongClick(baseRecyclerVH.itemView, linkedList.get(position),
                                position, baseRecyclerVH.getItemId());
                    }
                    return true;
                }
            });
        }
    }

    protected abstract void setVHData(RecyclerView.ViewHolder holder, Object data, int position);

    @Override
    public int getItemCount() {
        return linkedList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    /**
     * 重置链表数据
     *
     * @return
     */
    public void setList(LinkedList<?> list)
    {
        this.linkedList.clear();
        this.linkedList.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 在链表指定位置插入一个对象
     *
     * @return
     */
    public void addObject(int index, Object object) {
        if ((index >=0) && (index <= this.linkedList.size()) && (object != null))
        {
            this.linkedList.add(index, object);
            notifyItemInserted(index);
        }
    }

    /**
     * 在链表尾部添加一个对象
     *
     * @return
     */
    public void addObject(Object object) {
        if (object != null) this.linkedList.add(object);
        notifyItemInserted(linkedList.size());
    }

    /**
     * 在链表头部添加一个对象
     *
     * @return
     */
    public void addFirst(Object object) {
        if (object != null)
            this.linkedList.addFirst(object);
        notifyItemInserted(0);
    }

    /**
     * 在链尾部添加一个对象
     *
     * @return
     */
    public void addLast(Object object) {
        if (object != null) this.linkedList.addLast(object);
        notifyItemInserted(linkedList.size());
    }

    /**
     * 移除链表指定位置的对象
     *
     * @return
     */
    public void removeObject(int index) {
        if (index >= 0 && (index < this.linkedList.size())) {
            this.linkedList.remove(index);
            notifyItemRemoved(index);
        }
    }

    /**
     * 移除链表所有对象
     *
     * @return
     */
    public void removeAll() {
        this.linkedList.clear();
        notifyDataSetChanged();
    }

    /**
     * 获得链表
     *
     * @return 适配器链表对象
     */
    public LinkedList<Object> getList() {
        return this.linkedList;
    }
}
